<?php
class indexController extends baseController{
  protected $layout = "layout";
	public function index(){
		$this->title = model('default')->getTitle();
		$this->hello = model('default')->getHello();
		$this->display();
	}
  protected function _getChatConfig(){
    $config = file_get_contents(dirname(BASE_PATH).'/public/chat/js/config.json');
    return json_decode($config, true);
  }
  public function config(){   
    $this->data = $this->_getChatConfig();    
    $this->display();
  }
  public function getStatue(){
		$config = $this->_getChatConfig();
		$header = get_headers('http://'.$config['ip'].':'.$config['port'], 1);
    $status = $header[0];
    $state = "<font color='red'>通信失败</font>";
    if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
			if(intval($matches[1]) / 100 == 2){
				$state = "<font color='green'>通信成功</font>";  
			}
		}
		echo json_encode(array('state'=>$state));
  }
  public function chat(){
    $this->layout = "";
    $this->data = $this->data = $this->_getChatConfig();
    $this->title = array(
      8 => array('title'=>"神仙道8区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      7 => array('title'=>"神仙道7区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      6 => array('title'=>"神仙道6区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
    );
    $this->group = $_GET['group'];
    $this->uid = cp_uniqid();    
    $this->ip = rand(1000, 9999);    
    $this->display();
    $this->layout = "layout";
  } 
  
  public function pchat(){
    $this->layout = "";
    $this->data = $this->data = $this->_getChatConfig();
    $this->title = array(
      8 => array('title'=>"神仙道8区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      7 => array('title'=>"神仙道7区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      6 => array('title'=>"神仙道6区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
    );
    $this->group = $_GET['group'];
    $this->uid = $_GET['key'];    
    $this->uid2 = $_GET['key2'];    
    $this->nickname = $_GET['nickname'];  
    $this->display();
    $this->layout = "layout";
  } 
  
  public function tpl(){
    $this->layout = "";
    $this->title = array(
      8 => array('title'=>"神仙道8区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      7 => array('title'=>"神仙道7区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
      6 => array('title'=>"神仙道6区-玩家交流", 'gonggao'=>'大家可以互相撸管！'), 
    );
    $this->id = array(8, 7, 6);
    $tpl = $this->display('index_tpl', true);
    $this->layout = "layout";
    return $tpl;
  } 
  
  public function showFrame(){
    //为了解决iframe动态更改宽度，在跨域的情况下不能获取父页面的宽度
    $config = $this->_getChatConfig();
    $params = '';
    foreach($_GET as $k=>$v){
      if($k != 'r'){
        $params .= $k.'='.$v.'&';
      }
    }
    echo Http::doGet('http://'.$config['ip'].':'.$config['port'].'?'.$params);
  }
   
  public function getScript(){
    $p = $_POST['p'];
    $ip = $_POST['ip'];
    $port = $_POST['port'];
    $callback = $_POST['callback'];
    $pub = $_POST['pub'];
    $pub2 = $_POST['pub2'];
    $w = $_POST['w'];
    $h = $_POST['h'];
    file_put_contents(dirname(BASE_PATH).'/public/chat/js/config.json', json_encode(array('p'=>$p, 'ip'=>$ip, 'port'=>$port, 'callback'=>$callback, 'w'=>$w, 'h'=>$h, 'pub'=>$pub, 'pub2'=>$pub2)));
    $tpl = str_replace("\r\n", "", $this->tpl());
    ob_start();
    require_once(BASE_PATH . 'apps/chat/view/_js.php');
    $str = ob_get_contents();
    ob_end_clean();
    file_put_contents(dirname(BASE_PATH).'/public/chat/js/chat.js', $str);
    //$bat = 'if "%1" == "h" goto begin 
    //          mshta vbscript:createobject("wscript.shell").run("""%~fnx0"" h",0)(window.close)&&exit 
    //        :begin"';
    $bat = "set NODE_PATH=".BASE_PATH."apps/chat/inc/node_modules\r\n";
    $bat .= "cd ".BASE_PATH."apps/chat/inc\r\n";
    $bat .= "node $pub2/chat/js/server.js";
    file_put_contents(BASE_PATH.'/apps/chat/inc/nodeserver.bat', $bat);
    echo "更新成功，本app是需要jquery和artdialog的，请确保< script type='text/javascript' src='".__PUBLIC__."/chat/js/chat.js'>< /script> 放在您的页面底部";
  }
  
  public function execServer(){
    $statue = $_POST['t'];
    if($statue == 'start'){
      exec(BASE_PATH.'/apps/chat/inc/nodeserver.bat');
      
    }else if($statue == 'stop'){
      exec(BASE_PATH.'/apps/chat/inc/nodestop.bat');
    }else{
      exec(BASE_PATH.'/apps/chat/inc/nodestop.bat');
      exec(BASE_PATH.'/apps/chat/inc/nodeserver.bat');  
    }
  }
  
  public function execRedisServer(){
    $statue = $_POST['t'];
    if($statue == 'start'){
      exec(BASE_PATH.'/apps/chat/inc/redis-server.exe');
      
    }else if($statue == 'stop'){
      exec(BASE_PATH.'/apps/chat/inc/redisstop.bat');
    }else{
      exec(BASE_PATH.'/apps/chat/inc/redisstop.bat');
      exec(BASE_PATH.'/apps/chat/inc/redis-server.exe');           
    }
  }
}